//
//  WAYahooInfoProvider.m
//  WeatherApp
//
//  Created by Yuliya Grasevych on 02.09.14.
//  Copyright (c) 2014 Yuliya Grasevych. All rights reserved.
//

#import "WAYahooInfoProvider.h"
#import "NSURLRequest+RequestWithParameters.h"

@implementation WAYahooInfoProvider

+(WAYahooInfoProvider *)sharedInstance
{
    static WAYahooInfoProvider *_sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedInstance = [[WAYahooInfoProvider alloc] init];
    });
    return _sharedInstance;
}

-(void)yahooRequestWithStatement:(NSDictionary *)statement completionHandler:(void (^)(NSError *, NSDictionary *))completion
{
    NSURL *requestURL = [NSURL URLWithString:@"https://query.yahooapis.com/v1/public/yql"];
    NSURLRequest *request = [NSURLRequest requestWithURL:requestURL
                                              parameters:statement];
    [NSURLConnection sendAsynchronousRequest:request
                                       queue:[NSOperationQueue currentQueue]
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
                               
                               NSError *error = nil;
                               NSDictionary *requestResponse = nil;
                               if (!connectionError) {
                               requestResponse = [NSJSONSerialization JSONObjectWithData:data
                                                                                               options:NSJSONReadingAllowFragments
                                                                                                 error:&error];
                               }
                               completion([connectionError copy], requestResponse);
                           }];

}

@end
